home *** CD-ROM | disk | FTP | other *** search
- /*
- * http_dir.c: Handles the on-the-fly html index generation
- *
- * Rob McCool
- * 3/23/93
- *
- */
-
-
- /* httpd.h includes proper directory file */
- #include "httpd.h"
-
- struct ent {
- char *name;
- struct ent *next;
- };
-
-
- void output_directories(char **ar,int n,char *name,FILE *fd)
- {
- int x,lnum=0;
- char blorf[MAX_STRING_LEN];
-
- if(name[0] == '\0') {
- name[0] = '/'; name[1] = '\0';
- }
- /* aaaaargh Solaris sucks. */
- fflush(fd);
- for(x=0;x<n;x++) {
- if(strcmp(ar[x],".")) {
- fprintf(fd,"<LI> <A NAME=%d HREF=\"",++lnum);
- if(strcmp(ar[x],"..")) {
- make_full_path(name,ar[x],blorf);
- escape_url(blorf);
- fprintf(fd,"%s\">%s</A>",blorf,ar[x]);
- }
- else {
- make_full_path(name,"..",blorf);
- getparents(blorf);
- if(blorf[0] == '\0') {
- blorf[0] = '/'; blorf[1] = '\0';
- }
- escape_url(blorf);
- fprintf(fd,"%s\">%s</A>",blorf,"Parent Directory");
- }
- fputc(LF,fd);
- }
- }
- }
-
- int dsortf(char **s1,char **s2)
- {
- return(strcmp(*s1,*s2));
- }
-
-
- void index_directory(char *name, FILE *fd)
- {
- DIR *d;
- struct DIR_TYPE *dstruct;
- int num_ent=0,x;
- struct ent *head,*p,*q;
- char **ar;
- char unmunged_name[MAX_STRING_LEN];
-
- strcpy(unmunged_name,name);
- unmunge_name(unmunged_name);
-
- if(!(d=opendir(name)))
- die(FORBIDDEN,unmunged_name,fd);
-
- set_content_type(".html");
- if(!assbackwards)
- send_mime_headers(fd);
-
- if(header_only) return;
-
- /* Spew HTML preamble */
- fprintf(fd,"<TITLE>Index of %s</TITLE>",unmunged_name);
- fputc(LF,fd);
- fprintf(fd,"<H1>Index of %s</H1>",unmunged_name);
- fputc(LF,fd);
-
- fprintf(fd,"<UL>");
- fputc(LF,fd);
-
- /*
- * Since we don't know how many dir. entries there are, put them into a
- * linked list and then arrayificate them so qsort can use them.
- */
- head=NULL;
- while(dstruct=readdir(d)) {
- p=(struct ent *)malloc(sizeof(struct ent));
- p->name=strdup(dstruct->d_name);
- p->next=head;
- head=p;
- num_ent++;
- }
- ar=(char **) malloc(num_ent*sizeof(char *));
- p=head;
- x=0;
- while(p) {
- ar[x++]=p->name;
- p = p->next;
- }
-
- qsort((void *)ar,num_ent,sizeof(char *),
- #ifdef ULTRIX_BRAIN_DEATH
- (int (*))dsortf);
- #else
- (int (*)(const void *,const void *))dsortf);
- #endif
-
- output_directories(ar,num_ent,unmunged_name,fd);
- free(ar);
- q = head;
- while(q) {
- p=q->next;
- free(q->name);
- free(q);
- q=p;
- }
- closedir(d);
- fprintf(fd,"</UL>");
- }
-